package com.mbengue.corp.artisan.utils;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class IdGenerateur implements Serializable {
  private static final long serialVersionUID = 1519409269447690382L;
  private static ResourceBundle bundle;
  private static Connection connexion;
  private static Logger logger = Logger.getLogger(IdGenerateur.class);

  private final static String REQUETE_DERNIER_CODE_ARTICLE = "select code from article order by code desc limit 1";
  private final static String REQUETE_DERNIER_NUMERO_DEVIS = "select numero from feuille where type='Devis' order by numero desc limit 1";
  private final static String REQUETE_DERNIER_NUMERO_FACTURE = "select numero from feuille where type='Facture' order by numero desc limit 1";

  public enum Prefixe {
    ARTICLE, DEVIS, FACTURE
  }


  static {
    bundle = ResourceBundle.getBundle("connexionConfig");
    try {
      Class.forName(bundle.getString("mysql.driver"));
      MysqlDataSource dataSource = new MysqlDataSource();
      dataSource.setUrl(bundle.getString("mysql.db.url"));
      connexion = dataSource.getConnection(bundle.getString("mysql.user.name"), bundle.getString("mysql.user.pass"));
    } catch (ClassNotFoundException e) {
      logger.error("Le driver MySQL est introuvable", e);
    } catch (SQLException e) {
      logger.error("Probleme de connexion a la BD", e);
    }
  }

  public static String nouveauArticleId() {
    String nouveauId = nouvelIdentifiant(Prefixe.ARTICLE);
    if (nouveauId == null) {
      return "0001";
    }
    return nouveauId;
  }

  public static String nouveauDevisId() {
    String nouveauId = nouvelIdentifiant(Prefixe.DEVIS);
    if (nouveauId == null) {
      return "D0001";
    }
    return nouveauId;
  }

  public static String nouveauFactureId() {
    String nouveauId = nouvelIdentifiant(Prefixe.FACTURE);
    if (nouveauId == null) {
      return "F0001";
    }
    return nouveauId;
  }

  private static String nouvelIdentifiant(Prefixe prefixe) {
    logger.info("Generation d'un nouvel identifiant de type: " + prefixe);
    String requete;
    if (prefixe.equals(Prefixe.ARTICLE)) {
      requete = REQUETE_DERNIER_CODE_ARTICLE;
    } else if (prefixe.equals(Prefixe.DEVIS)) {
      requete = REQUETE_DERNIER_NUMERO_DEVIS;
    } else {
      requete = REQUETE_DERNIER_NUMERO_FACTURE;
    }

    try {
      Statement statement = connexion.createStatement();
      ResultSet res = statement.executeQuery(requete);
      if (res.next()) {
        int nouveauNum = prochainNumero(res.getString(1).substring(1));
        return nouvelleChaine(nouveauNum, prefixe);
      }
    } catch (SQLException e) {
      logger.error(e);
    }

    return null;
  }

  private static int prochainNumero(String numero) {
    return Integer.parseInt(numero) + 1;
  }

  private static String nouvelleChaine(int numero, Prefixe prefixe) {
    String suffixe = "";
    if (numero < 10) {
      suffixe = "000" + numero;
    } else if (numero >= 10 && numero < 100) {
      suffixe = "00" + numero;
    } else if (numero >= 100 && numero < 1000) {
      suffixe = "0" + numero;
    } else {
      suffixe += numero;
    }

    if (prefixe.equals(Prefixe.ARTICLE)) {
      return suffixe;
    } else if (prefixe.equals(Prefixe.DEVIS)) {
      return "D" + suffixe;
    } else {
      return "F" + suffixe;
    }
  }
}
